const util = require('../../utils/util');
const API = require('../../common/constant');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sexPick: {
      array: ['男', '女'],
      index: 0,
      isSexPick: false
    },
    Oldsign: ''
  },
  sexPickerChange: function (e) {
    console.log(e.detail.value);
    this.setData({
      ['sexPick.index']: e.detail.value,
      ['sexPick.isSexPick']: true
    })
  },
  bindDateChange: function (e) {
    this.setData({
      datePick: e.detail.value,
      isDatePick: true
    });
  },
  setSign: function (e) {

    this.setData({
      sign: e.detail.value,
      isSign: true
    });
    // console.log(this.data);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  linkBack: function (e) {
    console.log(e);
    let session = wx.getStorageSync('sessionId');
    if (session.length == 0) {
      app.showUserInfo('对不起您没有登录，没有权限修改资料', 'none', 1500);
    }
    // console.log(this.data);
    let bussData = this.data;
    if ((!bussData.sexPick.isSexPick) || (!bussData.isDatePick)) {
      wx.showToast({
        title: "请完善信息",
        icon: 'none',
        duration: 1500
      });
      if (!bussData.isSign) {

      }
    } else {
      util.http({
        url: API.URL.USER.EDIT_MINIUSERINFO,
        data: {
          sex: 1 + parseInt(bussData.sexPick.index),
          birthday: bussData.datePick,
          sign: bussData.sign
        },
        success: res => {
          wx.showToast({
            title: "修改资料成功",
            icon: 'none',
            duration: 1500
          });
          let obj = wx.getStorageSync('userInfo');
          obj.sex = bussData.sexPick.array[bussData.sexPick.index];
          wx.setStorageSync('userInfo', obj);
          console.log(wx.getStorageSync('userInfo'));
          setTimeout(() => {
            app.globalData.isNewMyInfo = true
            wx.navigateBack({})
          }, 1500);
        },
        fail: rej => {
          app.showUserInfo('修改信息失败', 'none', 1500);
        }
      });
    }
  },
  onLoad: function (options) {
    this.getMyInfo();
  },
  /**
   * 获取我的信息
   */
  getMyInfo: function () {
    util.http({
      url: API.URL.USER.FIND_USERINFO,
      success: res => {
        console.log(res.data.bussData.birthday);
        if (typeof (res.data.bussData.birthday) == 'undefined') {
          this.setData({
            ['sexPick.index']: res.data.bussData.sex == '女' ? 1 : 0,
            ['sexPick.isSexPick']: true,
            sign: '',
          });
        } else {
          this.setData({
            ['sexPick.index']: res.data.bussData.sex == '女' ? 1 : 0,
            ['sexPick.isSexPick']: true,
            datePick: res.data.bussData.birthday.split(" ")[0],
            isDatePick: true,
            sign: res.data.bussData.sign,
            isSign: true
          });
        }

        console.log(res.data.bussData);
      },
      fail: rej => {
        console.log(rej);
      }
    })
  },
})